package com.dr.job;


import java.util.Date;
import java.util.UUID;

import com.dr.common.pojo.AccessToken;
import com.dr.common.pojo.Ticket;
import com.dr.common.pojo.WeChatContent;
import com.dr.common.pojo.WeChatFinalValue;
import com.dr.common.ution.HttpClientUtil;
import com.dr.common.ution.JsonUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;


public class RefreshAccessTokenTask {
	
	@Value("${wechat_appid}")
	String wechat_appid;
	@Value("${wechat_appsecret}")
	String wechat_appsecret;
	@Value("${wechat_token}")
	String wechat_token;
	@Value("${wechat_base_url}")
	String wechat_base_url;
	
	static Logger logger =  Logger.getLogger(RefreshAccessTokenTask.class);
	
	public void refreshToken() {
		WeChatContent weChatConten = WeChatContent.getInstance();
		weChatConten.setAppId(wechat_appid);
		weChatConten.setAppSecurt(wechat_appsecret);
		weChatConten.setToken(wechat_token);
		weChatConten.setBaseUrl(wechat_base_url);
		String url = WeChatFinalValue.ACCESS_TOKEN_URL;
		url = url.replaceAll("APPID", wechat_appid);
		url = url.replaceAll("APPSECRET", wechat_appsecret);
		try {
			String content = HttpClientUtil.doGet(url);
			System.out.println("获取微信token:"+content);
			logger.info("获取微信token:"+content+"||"+new Date());
			AccessToken token = JsonUtil.toObject(content, AccessToken.class);
			//AccessToken token = new AccessToken();
			//token.setAccess_token("UtxmWhPunTE7GqWktzQcjv552ROuzRVANgocGUs33cMs9Q3S4ZlmHrV2mqkly4YsDt5YOSM2p7xdSGiissBXqDBbMWmQcpYWTPkHEu3LzlK-cpfeutcL8ttyJo5geDnqKOUdAEAAEK");
			//token.setExpires_in("7200");
			weChatConten.setAccessToken(token);
			if(token.getAccess_token() != null && token.getExpires_in() != null){
				String jsUrl = WeChatFinalValue.ACCESS_JS_TOKEN_URL;
				jsUrl = jsUrl.replace("ACCESS_TOKEN", WeChatContent.getInstance().getAccessToken().getAccess_token());
				String jsContent = HttpClientUtil.doGet(jsUrl);
				Ticket jsToken = JsonUtil.toObject(jsContent, Ticket.class);

				//AccessToken jsToken = new AccessToken();
				//jsToken.setAccess_token("UtxmWhPunTE7GqWktzQcjv552ROuzRVANgocGUs33cORBHfSdfR5nENRqhvuwcVVHDBF7d_wuL_GPZ8AVRoCZowgSj_ZQT2l1uCCF4AV_6GLsVPV01QlkJlL1W9PpYxIGBQdAEAQLA");
				//jsToken.setExpires_in("7200");
				weChatConten.setJsTicket(jsToken);
				System.out.println("获取微信js-token:"+jsContent);
				logger.info("获取微信js-token:"+content+"||"+new Date());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
		}
	}
}
